package com.cloud.demo.models.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.demo.common.cache.cachekey.UserCacheKeyBuild;
import com.cloud.demo.common.enums.ResultEnum;
import com.cloud.demo.common.web.exception.SysException;
import com.cloud.demo.models.sys.domain.UserDto;
import com.cloud.demo.models.sys.entity.SysUser;
import com.cloud.demo.models.sys.mapper.SysUserMapper;
import com.cloud.demo.models.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    private Long expire;
    @Resource
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisTemplate<String,String> stringRedisTemplate;
    @Override
    public String Login( UserDto userDto) {
        //非法登录检查
        expire = illegalLoginCheck(userDto);

        SysUser sysUser ;
        //String token  = UUID.randomUUID().toString().replace("-","");
        String key = new UserCacheKeyBuild().getKey(userDto.getUsername());
        String values = stringRedisTemplate.opsForValue().get(key);


        if (values!=null) {
            sysUser = JSON.parseObject(values, SysUser.class);
            checkPassword(sysUser,userDto);
            stringRedisTemplate.expire(key, 30,TimeUnit.MINUTES);

        }else {
            LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysUser::getLoginName,userDto.getUsername());
            sysUser = sysUserMapper.selectOne(wrapper);

            if (sysUser!=null){
                //todo 密码校验用md5 校验失败需要抛出异常
                checkPassword(sysUser,userDto);
                stringRedisTemplate.opsForValue().set(key,JSON.toJSONString(sysUser),30,TimeUnit.MINUTES);
            }
        }

        return key;

    }

    private void checkPassword(SysUser sysUser,UserDto userDto) {
        String illegal_loginKey = new UserCacheKeyBuild().getKey(userDto.getUsername(),"illegal","login");

        if (!userDto.getPassword().equals(sysUser.getPassword()) && expire!=null) {
            //非法登录

            stringRedisTemplate.opsForValue().set(
                    illegal_loginKey,
                    "3",
                    3,
                    TimeUnit.MINUTES);
            throw new SysException(ResultEnum.LOGIN_ERROR);
        }
    }

    private Long illegalLoginCheck(UserDto userDto) {
        String illegal_loginKey = new UserCacheKeyBuild().getKey(userDto.getUsername(),"illegal","login");
        String illegal_value = stringRedisTemplate.opsForValue().get(illegal_loginKey);

        if (illegal_value!=null){
            long L =  Long.parseLong(illegal_value);
            if (L>=0){
                stringRedisTemplate.opsForValue().decrement(illegal_loginKey,1);
                throw new SysException(ResultEnum.LOGIN_ERROR);
            }
        }




        Long expire = stringRedisTemplate.getExpire(illegal_loginKey);
        if (expire!=null &&expire>0){
            throw new SysException(ResultEnum.ILLEGAL_LOGIN);
        }
        return expire;
    }
}
